3 struct MapVertices: View {
8 let padding = CGFloat(5.0)
10 var onDragVertex: (Vertex, CGFloat, CGFloat) -> Void = { _, _, _ in }
13 ZStack(alignment: .topLeading) {
14 ForEach(vertices, id: \.id) { vertex in
15 ZStack(alignment: .topLeading) {
16 getVertexShape(vertex).fill(Color.map.vertexColor)
17 Text(vertex.label.replacingOccurrences(of: "\\n", with: "\n")).font(.theme.vertexLabel)
18 .foregroundColor(.map.labelColor)
19 .shadow(color: .white, radius: 0, x: -0.5, y: -0.5)
20 .shadow(color: .white, radius: 0, x: 0.5, y: 0.5)
23 width: w(vertex.position.x) + vertexSize.width + padding,
24 height: h(vertex.position.y) + 7.0))
28 let deltaX = value.startLocation.x - value.location.x
29 let deltaY = value.startLocation.y - value.location.y
30 onDragVertex(vertex, deltaX, deltaY)
37 func h(_ dimension: CGFloat) -> CGFloat {
38 max(0.0, min(mapSize.height, dimension * mapSize.height / 100.0))
41 func w(_ dimension: CGFloat) -> CGFloat {
42 max(0.0, min(mapSize.width, dimension * mapSize.width / 100.0))
45 func getVertexShape(_ vertex: Vertex) -> Path {
51 origin: CGPoint(x: w(vertex.position.x), y: h(vertex.position.y)), size: vertexSize
58 x: w(vertex.position.x), y: h(vertex.position.y), width: vertexSize.width,
59 height: vertexSize.height
64 path.move(to: CGPoint(x: w(vertex.position.x), y: h(vertex.position.y) + vertexSize.height))
67 x: w(vertex.position.x) + vertexSize.width, y: h(vertex.position.y) + vertexSize.height)
70 to: CGPoint(x: w(vertex.position.x) + vertexSize.width / 2.0, y: h(vertex.position.y)))
72 to: CGPoint(x: w(vertex.position.x), y: h(vertex.position.y) + vertexSize.height))
77 path.move(to: CGPoint(x: w(vertex.position.x), y: h(vertex.position.y)))
80 x: w(vertex.position.x) + vertexSize.width, y: h(vertex.position.y) + vertexSize.height)
83 path.move(to: CGPoint(x: w(vertex.position.x) + vertexSize.width, y: h(vertex.position.y)))
85 to: CGPoint(x: w(vertex.position.x), y: h(vertex.position.y) + vertexSize.height))
87 }.strokedPath(StrokeStyle(lineWidth: 2.0, lineCap: .butt))
94 mapSize: CGSize(width: 400.0, height: 400.0), vertexSize: CGSize(width: 25.0, height: 25.0),
96 Vertex(id: 0, label: "A Circle", position: CGPoint(x: 50.0, y: 50.0)),
97 Vertex(id: 1, label: "A Square", position: CGPoint(x: 10.0, y: 20.0), shape: .square),
98 Vertex(id: 2, label: "A triangle", position: CGPoint(x: 25, y: 32.0), shape: .triangle),
99 Vertex(id: 3, label: "An X", position: CGPoint(x: 70.0, y: 70.0), shape: .x),